package com.smartleanx.module.hrm.service.examine.examineResult;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.smartleanx.module.hrm.controller.admin.examine.examineResult.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResult.ExamineResultDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineResult.ExamineResultMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核结果模版 Service 实现类
 *
 * @author 
 */
@Service
@Validated
public class ExamineResultServiceImpl implements ExamineResultService {

    @Resource
    private ExamineResultMapper examineResultMapper;

    @Override
    public Integer createExamineResult(ExamineResultSaveReqVO createReqVO) {
        // 插入
        ExamineResultDO examineResult = BeanUtils.toBean(createReqVO, ExamineResultDO.class);
        examineResultMapper.insert(examineResult);
        // 返回
        return examineResult.getId();
    }

    @Override
    public void updateExamineResult(ExamineResultSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineResultExists(updateReqVO.getId());
        // 更新
        ExamineResultDO updateObj = BeanUtils.toBean(updateReqVO, ExamineResultDO.class);
        examineResultMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineResult(Integer id) {
        // 校验存在
        validateExamineResultExists(id);
        // 删除
        examineResultMapper.deleteById(id);
    }

    private void validateExamineResultExists(Integer id) {
        if (examineResultMapper.selectById(id) == null) {
            throw exception(EXAMINE_RESULT_NOT_EXISTS);
        }
    }

    @Override
    public ExamineResultDO getExamineResult(Integer id) {
        return examineResultMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineResultDO> getExamineResultPage(ExamineResultPageReqVO pageReqVO) {
        return examineResultMapper.selectPage(pageReqVO);
    }

}